package com.frejoys.task;

import com.frejoys.common.enums.EIsScore;
import com.frejoys.common.enums.EOrderType;
import com.frejoys.common.enums.EPayCurrency;
import com.frejoys.common.enums.EPayStatus;
import com.frejoys.common.enums.EPayType;
import com.frejoys.common.enums.EStoreActivityQueueStatus;
import com.frejoys.common.enums.EStoreActivityType;
import com.frejoys.common.enums.EVerify;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.dao.entity.StoreActivity;
import com.frejoys.dao.entity.StoreActivityQueue;
import com.frejoys.service.RebateService;
import com.frejoys.service.StoreActivityQueueService;
import com.frejoys.service.StoreActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

// 返利任务
@Slf4j
@Component
@RequiredArgsConstructor
public class RebateQueueTask {

    // @Scheduled(cron = "{秒数} {分钟} {小时} {日期} {月份} {星期}")

    private final RedissonClient redissonClient;

    private final RebateService rebateService;

    private final StoreActivityService storeActivityService;

    private final StoreActivityQueueService storeActivityQueueService;

    /**
     * 每2分钟，处理需要返利的信息
     * 将商品还原，订单失效
     */
    @Async
    // @Scheduled(cron = "0 */2 * * * ?")
    @Scheduled(cron = "0/5 * * * * ?")
    public void dispatchActivityQueue() {

        RLock lock = redissonClient.getLock(CacheKeyUtil.taskRebateQueueLockKey());

        if (lock.tryLock()) {
            try {

                Integer page = 1;
                Integer size = 1000;

                // 获取店铺活动
                while (true) {

                    // 获取进行中的店铺活动
                    List<StoreActivity> storeActivities = storeActivityService.getTaskValidActivity(page, size, EStoreActivityType.queueRebate.getKey());
                    if (storeActivities.isEmpty()) {
                        break;
                    }

                    for (StoreActivity e : storeActivities) {

                        // 排队返利
                        // 返利排名第一的用户
                        StoreActivityQueue queue = storeActivityQueueService.getFirstSimpleInfo(e.getId(), EStoreActivityQueueStatus.wait.getValue());

                        if (queue != null) {

                            try {
                                rebateService.activityQueue(queue, e.getStoreId(), e.getMoney(), e.getRebateMaxMoney());
                            }catch (Exception exception) {
                                // exception.printStackTrace();
                                log.error("返利任务异常：", exception);
                            }

                        }

                    }


                    page++;
                }

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        }

    }











}
